<?php
/**
 * Created by PhpStorm.
 * User: 李闯
 * Date: 2020/5/21
 * Time: 9:53
 */

namespace app\index\controller;

use Protocol\Curl;
use Redis\Redis;
use think\Db;
use think\Request;

class Biznews extends Common
{

    function getInfo(){
        $topic = ['E联车服'];
        $title = Db::table('biz')->where(array('id'=>$this->bizId))->value('biz_title');
        return array('status' => true, 'topic' => $topic,'title'=>$title);
    }
    function addVideoIntegral()
    {
        $biz_id = input('post.id');
        $member_id  = input('post.member_id');
        $share_id  = input('post.share_id');
        Db::table('share_video')
            ->insert(array(
                'member_id'=>$member_id,
                'b_id'=>$biz_id,
                'share_id'=>$share_id,
                'add_time'=>date('Y-m-d H:i:s'),
                'integral_num'=>0
            ));
    }
    function share_id()
    {
        $share_id='';
        $client_token_url = 'https://open.douyin.com/oauth/client_token/';
        $order = [
            'client_key' => 'awat9we7skb9anza',
            'client_secret' => 'f95d24fec8b15c2d5b9e2ac91fcda68a',
            'grant_type' => 'client_credential'
        ];
        $curl = new Curl();
        $res = $curl->post($client_token_url, $order, 'form');
        $resData = json_decode($res, true);
        if (!empty($resData)) {
            if ($resData['data']['error_code'] == 0) {
                $token = $resData['data']['access_token'];
                $url = 'https://open.douyin.com/share-id/?access_token='.$token.'&need_callback=true';
                $re = $curl->get($url);
                $reData = json_decode($re, true);
                if($reData['data']['error_code']==0){
                    $share_id=$reData['data']['share_id'];
                }
            }
        }
        return array("status" => true, 'share_id'=>$share_id);;
    }
    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 请求门店消息列表
     */
    function bizNewsList()
    {
        #查询消息
        $info = Db::table('biz_news bn')->field("bn.id news_id,bn.content,bn.type,if(bnm.status = 1,'未读','已读'),bn.create_time time")
            ->join('biz_news_mapping bnm', 'bnm.biz_news_id = bn.id')
            ->where("bnm.biz_id = $this->bizId")
            ->order('bnm.status asc')->select();
        if (!empty($info)) {
            return array('status' => true, 'data' => $info);
        } else {
            return array('status' => false);
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 删除消息
     */
    function bizNewsDel()
    {
        $type = input("post.type");
        $news_id = input("post.news_id");
        if ($type == 1 or $type==3) {
            #群发消息
            $info = Db::table('biz_news_mapping')->where("biz_news_id = $news_id and biz_id = $this->bizId ")->delete();
        } else {
            #通知消息
            $info = Db::table('biz_news')->where("id = $news_id and type = 2")->delete();
        }
        if ($info) {
            return array('status' => true);
        } else {
            return array('status' => false);
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 查看已读消息
     */
    function bizNewsRead()
    {
        $news_id = input("post.news_id");
        if (!empty($news_id)) {
            $info = Db::table('biz_news_mapping')
                ->where("biz_id = $this->bizId and biz_news_id = $news_id")
                ->update(array('status' => 2));
            return array('status' => true);
        }
    }
}
